import Axios from 'axios';
import {IModels} from './index.d';
import {env} from '../env';

console.log(env);

const http = Axios.create({
  baseURL: 'http://api.com', // 请求后端接口的baseurl
  timeout: 100000, // 设置请求的超时时间
});

// 请求拦截器
http.interceptors.request.use(
  config => {
    // 例如
    config.headers.Authorization = ``;
    return config;
  },
  err => {
    return Promise.reject(err);
  },
);

// 响应拦截器
http.interceptors.response.use(
  response => {
    return response.data;
  },
  err => {
    return Promise.reject(err);
  },
);

type Fetch = <T>(
  url: keyof IModels,
  params?: Record<string, any>,
  hideMsg?: boolean,
) => Promise<T>;
export const fetchFun: Fetch = (url, params) => {
  const [method, ...urlArr] = url.split('/');
  const requestUrl = urlArr.join('/');
  if (method === 'GET') {
    return http.get(requestUrl, params);
  }
  return http.post(requestUrl, params);
};
